package com.shanzmoo.open.server.api.manager;

import cn.hutool.core.lang.Assert;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.json.JSONUtil;
import com.shanzmoo.db.module.merchant.entity.MerchantInfoEntity;
import com.shanzmoo.db.module.merchant.entity.MerchantOrderEntity;
import com.shanzmoo.db.module.merchant.service.IMerchantInfoService;
import com.shanzmoo.db.module.merchant.service.IMerchantOrderService;
import com.shanzmoo.db.module.pay.entity.SPayOrderInfoEntity;
import com.shanzmoo.db.module.pay.service.ISPayOrderInfoService;
import com.shanzmoo.open.server.api.domain.request.MerchantCallBackRequest;
import com.shanzmoo.open.server.api.domain.request.PayCallBackRequest;
import com.shanzmoo.open.server.api.domain.vo.MerchantCallBackVo;
import com.shanzmoo.open.server.api.domain.vo.PayCallBackVo;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import com.shanzmoo.pay.util.*;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author dingmeng
 * @title: PayCallBackManager
 * @projectName jyf-mall-server
 * @description: 扫呗支付回调
 * @date 2021/4/1614:34
 */
@Component
@RequiredArgsConstructor
public class PayCallBackManager {

    private final ISPayOrderInfoService iSPayOrderInfoService;
    private final IMerchantOrderService iMerchantOrderService;
    // 入驻商户操作服务
    private final IMerchantInfoService iMerchantInfoService;

    /**
     * 扫呗支付回调方法，更新业务单，支付单信息
     *
     * @param httpServletRequest
     * @return
     */
    @SneakyThrows
    public PayCallBackVo payCallBack(HttpServletRequest httpServletRequest) {
        String body = ServletUtil.getBody(httpServletRequest);
        PayCallBackRequest request = JSONUtil.toBean(body, PayCallBackRequest.class);
        PayCallBackVo vo = new PayCallBackVo();
        SPayOrderInfoEntity payOrderInfoEntity = iSPayOrderInfoService
                .lambdaQuery()
                .eq(SPayOrderInfoEntity::getPayOrderCode, request.getTerminal_trace())
                .one();
        Assert.notNull(payOrderInfoEntity, request.getTerminal_trace() + "支付订单不存在");
        MerchantOrderEntity merchantOrderEntity = iMerchantOrderService
                .lambdaQuery()
                .eq(MerchantOrderEntity::getPayOrderCode, request.getOut_trade_no())
                .one();
        Assert.notNull(merchantOrderEntity, request.getOut_trade_no() + "所属业务订单不存在");
        //业务响应成功
        if (request.getResult_code().equals(Constants.RESULT_CODE_SUCCESS)) {
            payOrderInfoEntity.setPayTime(LocalDateTime.parse(request.getEnd_time(), DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
            //支付成功
            payOrderInfoEntity.setPayStatus(Constants.PAY_STATUS_SUCCESS);
            merchantOrderEntity.setPayTime(LocalDateTime.parse(request.getEnd_time(), DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
            merchantOrderEntity.setPayStatus(Constants.PAY_STATUS_SUCCESS.toString());
            merchantOrderEntity.setStatus(1);
        } else {
            //支付失败
            payOrderInfoEntity.setPayStatus(Constants.ORDER_STATUS_FAIL);
            merchantOrderEntity.setPayStatus(Constants.ORDER_STATUS_FAIL.toString());
            merchantOrderEntity.setStatus(3);
        }
        //保存支付单
        iSPayOrderInfoService.saveOrUpdate(payOrderInfoEntity);
        iMerchantOrderService.saveOrUpdate(merchantOrderEntity);
        vo.setReturn_code(Constants.RESULT_CODE_SUCCESS);
        vo.setReturn_msg("success");
        return vo;
    }

    /**
     * 扫呗创建商户回调方法，更新商户状态
     *
     * @param httpServletRequest
     * @return
     */
    @SneakyThrows
    public MerchantCallBackVo merchantCallBack(HttpServletRequest httpServletRequest) {
        String body = ServletUtil.getBody(httpServletRequest);
        MerchantCallBackRequest request = JSONUtil.toBean(body, MerchantCallBackRequest.class);
        MerchantCallBackVo vo = new MerchantCallBackVo();
        // 1、获取商户基本信息
        MerchantInfoEntity merchantInfoEntity = iMerchantInfoService.lambdaQuery()
                .eq(MerchantInfoEntity::getCode, request.getMerchant_no())
                .one();
        //商户审核通过
        if (request.getResult_code().equals(Constants.RESULT_CODE_SUCCESS)) {
            merchantInfoEntity.setStatus("3");
        } else if (request.getResult_code().equals(Constants.RESULT_CODE_FAIL)) {
            merchantInfoEntity.setStatus("4");
        }
        //保存商户状态
        iMerchantInfoService.saveOrUpdate(merchantInfoEntity);
        vo.setReturn_code(Constants.RESULT_CODE_SUCCESS);
        vo.setReturn_msg("success");
        vo.setTrace_no(request.getTrace_no());
        return vo;
    }
}
